package com.jeecms.wicool.dao.car.impl;


import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import com.jeecms.cms.entity.main.CmsUser;
import com.jeecms.common.hibernate3.Finder;
import com.jeecms.common.hibernate3.HibernateBaseDao;
import com.jeecms.common.page.Pagination;
import com.jeecms.wicool.dao.car.CarOrderConfDao;
import com.jeecms.wicool.entity.car.CarOrderConf;

@Repository
public class CarOrderConfDaoImpl extends HibernateBaseDao<CarOrderConf, Integer>
		implements CarOrderConfDao {

	@Override
	protected Class<CarOrderConf> getEntityClass() {
		// TODO Auto-generated method stub
		return CarOrderConf.class;
	}
	@Override
	public Pagination getPage(Short orderType,String name,int pageNo, int pageSize,CmsUser user) {
			Finder f = Finder.create("select bean from CarOrderConf bean where 1=1 ");
			if (orderType!=null){
				f.append(" and bean.orderType = :orderType");
				f.setParam("orderType",  orderType );
			}if (!StringUtils.isBlank(name)){
				f.append(" and bean.name = :name");
				f.setParam("name",name);
			}if(user.getShopId()!=null){
				f.append(" and bean.shop.id = :shopId");
				f.setParam("shopId",user.getShopId());
			}
			f.append(" order by bean.id desc");
			return find(f, pageNo, pageSize);
	}
	@Override
	public CarOrderConf findById(Integer id) {
		CarOrderConf entity = get(id);
		return entity;
	}
	@Override
	public CarOrderConf save(CarOrderConf bean) {
		getSession().save(bean);
		return bean;
	}
	@Override
	public CarOrderConf deleteById(Integer id) {
		CarOrderConf entity = super.get(id);
		if (entity != null) {
			getSession().delete(entity);
			getSession().flush();
		}
		return entity;
	}
	@Override
	public List<CarOrderConf> getAll(CmsUser user) {
		Finder f= Finder.create("select bean from CarOrderConf bean where 1=1 ");
		if(user.getShopId()!=null){
			f.append(" and bean.shop.id = :shopId");
			f.setParam("shopId",user.getShopId());
		}
		f.append(" order by bean.id desc");
		return find(f);
	}
}